﻿Imports System.Text.RegularExpressions
Imports System.Data.SqlClient

Public Class UIAttribute
    Private Const pattern As String = "(?<label>[a-zA-Z].+)*{(?<type>[a-z]+){(?<meta>.+)}}"
    Public Property AttributeType As UIAttributeEnumeration
    Public Property Label As String
    Public Property AttributeData As Object

    Public Sub New(Comment As String)
        Dim match As Match = Regex.Match(Comment, pattern, RegexOptions.IgnoreCase)

        Dim labelGroup As Group = match.Groups("label")
        Dim typeGroup As Group = match.Groups("type")
        Dim metaGroup As Group = match.Groups("meta")

        If Not IsNothing(labelGroup) AndAlso labelGroup.Success Then
            Label = labelGroup.Value
        End If

        If Not IsNothing(typeGroup) AndAlso typeGroup.Success Then
            Select Case typeGroup.Value.ToLower
                Case "sqldropdown"
                    AttributeType = UIAttributeEnumeration.SqlDropDown
                    If Not IsNothing(SqlConnectionContext.Current) AndAlso SqlConnectionContext.Current.State = ConnectionState.Open AndAlso metaGroup.Success Then
                        Dim result As New List(Of KeyValuePair(Of String, Object))
                        Using cmd As New SqlCommand(metaGroup.Value, SqlConnectionContext.Current)
                            Using dr As SqlDataReader = cmd.ExecuteReader
                                While dr.Read
                                    result.Add(New KeyValuePair(Of String, Object)(dr(0), dr(1)))
                                End While
                            End Using
                        End Using
                        AttributeData = result
                    ElseIf metaGroup.Success Then
                        AttributeData = metaGroup.Value
                    End If
                Case "dropdown"
                    AttributeType = UIAttributeEnumeration.DropDown
                    If metaGroup.Success Then
                        Dim contents As String = metaGroup.Value
                        Dim ddList As New List(Of KeyValuePair(Of String, Object))
                        Dim items As String() = contents.Split(",")
                        For Each item As String In items
                            Dim kv As String() = item.Split(":")
                            ddList.Add(New KeyValuePair(Of String, Object)(kv(0), kv(1)))
                        Next
                        AttributeData = ddList
                    End If
                Case "filefilter"
                    AttributeType = UIAttributeEnumeration.FileFilter
                    If metaGroup.Success Then
                        AttributeData = metaGroup.Value
                    End If
                Case "display"
                    AttributeType = UIAttributeEnumeration.Display
                    If metaGroup.Success Then
                        Select Case metaGroup.Value.ToLower
                            Case "datagrid"
                                AttributeData = DisplayAttributeEnumeration.DataGrid
                            Case "piechart"
                                AttributeData = DisplayAttributeEnumeration.PieChart
                            Case "columnchart"
                                AttributeData = DisplayAttributeEnumeration.ColumnChart
                            Case "areachart"
                                AttributeData = DisplayAttributeEnumeration.AreaChart
                            Case "barchart"
                                AttributeData = DisplayAttributeEnumeration.BarChart
                            Case "linechart"
                                AttributeData = DisplayAttributeEnumeration.LineChart
                            Case "bubblechart"
                                AttributeData = DisplayAttributeEnumeration.BubbleChart
                            Case "scatterchart"
                                AttributeData = DisplayAttributeEnumeration.ScatterChart
                        End Select
                    End If
            End Select
        End If
    End Sub
End Class
